package logic

import (
	"context"
	"errors"
	"user_srv/models/mysql"
	"user_srv/pkg"

	"user_srv/internal/svc"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealNameLogic {
	return &RealNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RealNameLogic) RealName(in *user.RealNameRequest) (*user.RealNameResponse, error) {
	// todo: add your logic here and delete this line
	name := mysql.RealName{}
	err := name.RealNameInfo(in.UserId, in.RealName)
	if err != nil {
		logx.Info("获取实名认证错误")
		return nil, errors.New("获取实名认证错误")
	}
	if name.ID != 0 {
		logx.Info("实名认证已实名")
		return nil, errors.New("实名认证已实名")
	}
	if !pkg.RealNameAuthentication(in.IdCord, in.RealName) {
		logx.Info("第三方实名认证信息失败")
		return nil, errors.New("第三方实名认证信息失败")
	}
	name.UserId = in.UserId
	name.RealName = in.RealName
	name.IdCord = in.IdCord
	err = name.Create()
	if err != nil {
		logx.Info("保存实名认证信息失败")
		return nil, errors.New("保存实名认证信息失败")
	}
	return &user.RealNameResponse{
		RealNameId: int64(name.ID),
	}, nil
}
